<?php
/**
 * Created by PhpStorm.
 * User: eugene
 * Date: 2018/4/23
 * Time: 17:18
 */

namespace App\Models\Merchant;


use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Gift extends Model
{
    protected $table='merchant_gift';
    
    //protected static function boot()
    //{
    //    parent::boot();
    //
    //    //效果未失效
    //    static::addGlobalScope('opened', function(Builder $builder) {
    //        $builder->where('is_open', 1);
    //    });
    //
    //}
    
    public function scopeShow($query)
    {
        return $query->where('show',1);
    }
    
    
    public function scopeValid($query)
    {
        return $query->where('begin_datetime','<',Carbon::now()->toDateTimeString())
            ->where('end_datetime','>',Carbon::now()->toDateTimeString())
            ->where('is_open', 1)
            ->where('stock','>',0);
    }
    
    public function getImgAttribute()
    {
        if(!$this->attributes['img'])return '';
        return starts_with($this->attributes['img'],'http') ? $this->attributes['img'] : config('filesystems.disks.admin.url').$this->attributes['img'];
    }
    
}